<?
require_once ('Core/Auth/Type.php');
class Core_Auth
{
    /**
     * This factory method creates the appropriate authentication adapter for the given user type.
     *
     * @param string $type
     * @return Zend_Auth_Adapter_Interface
     */
    public static function factory ($type, $params = array())
    {
        // Switch on registration type
        switch ($type) {
            case Core_Auth_Type::GOOGLE:
            case Core_Auth_Type::TWITTER:
            case Core_Auth_Type::FACEBOOK:
            case Core_Auth_Type::LINKEDIN:
            case Core_Auth_Type::ETSY:
            case Core_Auth_Type::NATIVE:
                if (file_exists("Core/Auth/Adapter/{$type}.php"))
                {
                    
                }
            default:
                return self::nativeCreateHelper($login, $password);
        }
    }
    ########################## Utility Helpers ##########################
    /**
     * Adapter creation helper for facebook.
     * 
     * @return Zend_Auth_Adapter_Interface
     */
    private static function facebookCreateHelper ()
    {
        // Get the facebook config
        $fbConfig = Core_Config::read('facebook');
        // Get our authentication adapter
        return new Core_Auth_Adapter_Facebook($fbConfig->appId, 
        $fbConfig->secret, $fbConfig->redirect, $fbConfig->scope);
    }
    /**
     * Adapter creation helper for twitter.
     * 
     * @return Zend_Auth_Adapter_Interface
     */
    private static function twitterCreateHelper ()
    {
        // Get the facebook config
        $twitterConf = Core_Config::read('twitter');
        // Get our authentication adapter and check credentials
        return new Core_Auth_Adapter_OAuth($twitterConf->oauth);
    }
    /**
     * Adapter creation helper for linkedin.
     * 
     * @return Zend_Auth_Adapter_Interface
     */
    private static function linkedinCreateHelper ()
    {
        // Get the facebook config
        $linkedinConf = Core_Config::read('linkedin');
        // Get our authentication adapter and check credentials
        return new Core_Auth_Adapter_OAuth($linkedinConf->oauth);
    }
    /**
     * Adapter creation helper for etsy.
     * 
     * @return Zend_Auth_Adapter_Interface
     */
    private static function etsyCreateHelper ()
    {
        // Get the etsy config
        $etsyConfig = Core_Config::read('etsy');
        // Get our authentication adapter and check credentials
        return new Core_Auth_Adapter_OAuth($etsyConfig->oauth);
    }
    /**
     * Adapter creation helper for the google OpenId registration.
     * 
     * @return Zend_Auth_Adapter_Interface
     */
    private function googleCreateHelper ()
    {
        $googleConf = Core_Config::read('google');
        // Create the adapter.
        $sreg = new Zend_OpenId_Extension_Sreg(
        Zend_OpenId_Extension_Sreg::getSregProperties(), 
        $googleConf->openid->policyUrl, $googleConf->openid->version);
        $adapter = new Core_Auth_Adapter_OpenId();
        $adapter->setExtensions($sreg);
        $adapter->setIdentityUrl($googleConf->openid->identityUrl);
        $adapter->setReturnTo($googleConf->openid->returnUrl);
        return $adapter;
    }
    /**
     * Adapter creation helper, using the Core DB Adapter
     * 
     * @return Zend_Auth_Adapter_Interface
     */
    private static function nativeCreateHelper ($login, $password)
    {
        // Get our authentication adapter and check credentials
        $frontController = Zend_Controller_Front::getInstance();
        $bootstrap = $frontController->getParam('bootstrap');
        $db = $bootstrap->getResource('db');
        $adapter = new Zend_Auth_Adapter_DbTable($db);
        $adapter->setTableName("user");
        $adapter->setIdentityColumn('login');
        $adapter->setCredentialColumn('password');
        $adapter->setCredentialTreatment("SHA1( CONCAT( ?, salt ) )");
        $adapter->setIdentity($login);
        $adapter->setCredential($password);
        return $adapter;
    }
}